package com.lgkj.ui;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;

import com.lgkj.R;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.CommonUtils;
import com.lgkj.utils.HttpUtils;
import com.lgkj.utils.SharedPreferencesUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/7/6 0006.
 */

public class ControlActivity extends BaseActivity {
    @BindView(R.id.back)
    ImageView back;
    private String ip;

    @Override
    public int getLayoutId() {
        return R.layout.activity_control;
    }

    @Override
    public void initView() {
    }

    @Override
    public void initData() {
        ip = SharedPreferencesUtils.getParam(ControlActivity.this, "ip", "").toString();
        if (!CommonUtils.startPing(ip)) {
            toastLong("当前没在同一个网络，请先切换wifi", 0);
        }
    }


    @OnClick({R.id.ibtn_ok, R.id.ibtn_top, R.id.ibtn_buttom, R.id.ibtn_left, R.id.ibtn_right, R.id.ibtn_jia, R.id.ibtn_add, R.id.ibt_back, R.id.btn_save, R.id.back, R.id.item1, R.id.item2, R.id.item3, R.id.item4})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ibtn_ok:
                control("sure");
                break;
            case R.id.ibtn_top:
                control("up");
                break;
            case R.id.ibtn_buttom:
                control("down");
                break;
            case R.id.ibtn_left:
                control("left");
                break;
            case R.id.ibtn_right:
                control("right");
                break;
            case R.id.ibtn_jia:
                control("voice_less");
                break;
            case R.id.ibtn_add:
                control("voice_add");
                break;
            case R.id.ibt_back:
                control("back");
                break;
            case R.id.btn_save:
//                control("sure");
                break;
            case R.id.back:
                finish();
                break;
            case R.id.item1:
                controlSb("item1");
                break;
            case R.id.item2:
                controlSb("item2");
                break;
            case R.id.item3:
                startsActivity(new Intent(ControlActivity.this, NewListActivity.class));
                break;
            case R.id.item4:
                startsActivity(new Intent(ControlActivity.this, MovieActivity.class));
                break;
        }
    }

    private void control(String order) {
        showDiaLog("");
        final String phone = SharedPreferencesUtils.getParam(ControlActivity.this, "phone", "").toString();
        Map<String, String> map = new HashMap<>();
        map.put("phone", phone);
        map.put("contral", order);
        HttpUtils.getHttp(map, ip + CommonConstant.control, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                if (jsonObject.optInt("code") == 0) {

                }
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
                toastLong("当前不是一个wifi，请选链接一个wifi", 0);
            }
        });
    }

    private void controlSb(String order) {
        showDiaLog("");
        final String phone = SharedPreferencesUtils.getParam(ControlActivity.this, "phone", "").toString();
        Map<String, String> map = new HashMap<>();
        map.put("phone", phone);
        map.put("contral", "");
        map.put("open", order);
        HttpUtils.getHttp(map, ip + CommonConstant.control, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                if (jsonObject.optInt("code") == 0) {

                }
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
                toastLong("当前不是一个wifi，请选链接一个wifi", 0);
            }
        });
    }


}
